package com.xml.actions;


import com.opensymphony.xwork2.ActionSupport;
import com.xml.domain.Book;
import com.xml.domain.Borrow;
import com.xml.domain.Reader;
import com.xml.services.BookService;
import com.xml.services.BookServiceImpl;
import com.xml.services.BorrowService;
import com.xml.services.BorrowServiceImpl;
import com.xml.services.ReaderService;
import com.xml.services.ReaderServiceImpl;

public class BorrowAction extends ActionSupport{
	private Reader reader;
	private Book book;
	private Borrow borrow;
	private ReaderService rs;
	private BookService bs;
	private BorrowService borrowService;
	
	
	public String borrowBook(){
		boolean flag1;
		boolean flag2;
		boolean flag3;
		int maxBorrow = 5;
		rs = new ReaderServiceImpl();
		bs = new BookServiceImpl();
		borrowService = new BorrowServiceImpl();
		flag1 = rs.checkReader(reader.getReaderId());
		flag2 = bs.checkBook(book.getIsbn());
		if(!flag1){
			return "readerERROR";}
		if(!flag2){
			return "bookERROR";
		}
		if(rs.numOfBorrow(reader.getReaderId())>=maxBorrow)
			return "overBorrow";
		flag3 = borrowService.borrowBook(reader.getReaderId(), book.getIsbn());
		if(flag3)
			return SUCCESS;
		else
			return INPUT;
	}
	
	public String returnBook(){
		boolean flag;
		borrowService = new BorrowServiceImpl();
		flag = borrowService.returnBook(reader.getReaderId(), book.getIsbn());
		if(flag)
			return SUCCESS;
		else
			return INPUT;
	}
	
//	public String searchBorrowInfo(){
//		
//		
//		return SUCCESS;
//	}


	public Reader getReader() {
		return reader;
	}


	public void setReader(Reader reader) {
		this.reader = reader;
	}


	public Book getBook() {
		return book;
	}


	public void setBook(Book book) {
		this.book = book;
	}


	public Borrow getBorrow() {
		return borrow;
	}


	public void setBorrow(Borrow borrow) {
		this.borrow = borrow;
	}


	public ReaderService getRs() {
		return rs;
	}


	public void setRs(ReaderService rs) {
		this.rs = rs;
	}

	public BookService getBs() {
		return bs;
	}

	public void setBs(BookService bs) {
		this.bs = bs;
	}

	public BorrowService getBorrowService() {
		return borrowService;
	}

	public void setBorrowService(BorrowService borrowService) {
		this.borrowService = borrowService;
	}
	
	

}
